package com.project.controller;


import com.Entity.DzmPatientEntity;
import com.alibaba.dubbo.config.annotation.Reference;
import gz.PatientService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(value = {"*"},allowCredentials = "true")
public class PatientController {

    @Reference
    private PatientService patientServiceImp;

    /**
     * 查询所有正常的患者(可根据姓名和电话模糊查询)
     * @return
     */
    @RequestMapping("allPatient")
    public List<DzmPatientEntity> all(String name, String mobile){
        System.out.println("返回模糊查询参数姓名和电话："+name+"*******"+mobile);
        List<DzmPatientEntity>  all = patientServiceImp.findAll(name,mobile);
        return all;
    }


    /**
     * 查询所有被移除的患者显示表格(根据姓名模糊查询)
     * @return
     */
    @RequestMapping("findss")
    public List<DzmPatientEntity> allmove(String names){
        System.out.println("返回模糊查询参数姓名："+names+"*******");
        List<DzmPatientEntity>  all = patientServiceImp.allMove(names);
        System.out.println("模糊查询结果："+all);
        return all;
    }


    //查询被移除的患者总人数
    @RequestMapping("movePerson")
    public int person(){
       int i =  patientServiceImp.movePerson();
        System.out.println("移出的人数："+i);
            return i;
    }




    /**
     * 点击移除按钮添加到被移除的患者表中
     * @param request
     * @return
     */
    @RequestMapping("set")
    public Map<String, Object> move(HttpServletRequest request){
        String patient_id = request.getParameter("patient_id");
        Integer id = Integer.valueOf(patient_id);
        System.out.println(id+"*******");
        int i = patientServiceImp.moveId(id);
        int a =  patientServiceImp.movePerson();
        System.out.println(i+"行受影响");
        System.out.println(a+"移除人数");
        Map<String,Object> map = new HashMap<>();
        map.put("msg","true");
        map.put("person",a);
        return map;
    }


    /**
     * 批量删除
     * @param patient_id
     * @return
     */
    @RequestMapping("deletes")
    public Map<String, Object> delete(String[] patient_id){
        System.out.println("选中删除的patient_id有："+patient_id);
        int i =   patientServiceImp.deletes_patient(patient_id);
        int s =  patientServiceImp.movePerson();
        System.out.println(i+"行受影响");
        Map<String,Object> map = new HashMap<>();
        map.put("msg","true");
        map.put("person",s);
        return map;
    }

    /**
     * 批量恢复数据
     * @param patient_id
     * @return
     */
    @RequestMapping("recover")
    public Map<String, Object> recover(String[] patient_id){
        System.out.println("选中恢复的patient_id有："+patient_id);
        int i =   patientServiceImp.recover_patient(patient_id);
        System.out.println(i+"行受影响");
        int s =  patientServiceImp.movePerson();
        Map<String,Object> map = new HashMap<>();
        map.put("msg","true");
        map.put("person",s);
        return map;
    }


    /**
     * 点击编辑按钮获取患者信息
     * @param request
     * @return
     */
    @RequestMapping("getOne")
    public Map<String,Object> ready(HttpServletRequest request){
        String id = request.getParameter("patient_id");
        System.out.println("准备修改id:"+id);
        Integer patient_id = Integer.valueOf(id);
        List<Map<String,Object>> all = null;
            all = patientServiceImp.getOne(patient_id);
        System.out.println("获取的信息:"+all);
        Map<String,Object> map = new HashMap<>();
        if(all!=null&&all.size()>0){
            map.put("msg","true");
            map.put("all",all);
        }else{
            all = patientServiceImp.getOnes(patient_id);
            map.put("all",all);
        }
        return map;
    }


    /**
     * 根据返回的map数据对表数据进行修改
     * @param map
     * @return
     */
    @RequestMapping("patient_update")
    public Map<String,Object> update(@RequestParam Map<String,Object> map){
        System.out.println("返回的map集合："+map);
        int a = patientServiceImp.insert_patientFile(map);
        int m = patientServiceImp.insert_file(map);
        System.out.println(a+m+"行受影响");
        Map<String,Object> maps = new HashMap<>();
        if(m>0&&a>0){
            maps.put("msg","true");
        }else{
            maps.put("msg","false");
        }
        return maps;
    }
}
